package com.kefu.boss.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.boss.model.BossMenu;
import com.kefu.boss.model.BossRole;
import com.kefu.boss.model.BossRoleMenu;
import com.kefu.boss.service.BossMenuService;
import com.kefu.boss.service.BossRoleMenuService;
import com.kefu.boss.service.BossRoleService;
import com.kefu.boss.service.BossUserRoleService;
import com.kefu.boss.vo.BossRoleInsertVo;
import com.kefu.boss.vo.BossRoleSearchVo;
import com.kefu.boss.vo.BossRoleUpdateVo;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.util.Assert;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "业务支撑角色")
@RestController
@RequestMapping("boss/role")
public class BossRoleController extends AbstractBaseController {
    @Resource
    protected BossRoleService bossRoleService;
    @Resource
    protected BossRoleMenuService bossRoleMenuService;
    @Resource
    private BossMenuService bossMenuService;
    @Resource
    private BossUserRoleService bossUserRoleService;

    @PostMapping("/add")
    @ApiOperation(value = "新增角色", notes = "新增角色")
    public Result<String> add(@Validated @RequestBody BossRoleInsertVo bossRoleVo) {
        logger.info("新增角色:{}", bossRoleVo);

        // 角色菜单不能为空
        if(bossRoleVo.getMenuList()==null||bossRoleVo.getMenuList().size()==0) {
        	throw BusinessException.msg("角色菜单不能为空");
        }
        BossRole info = bossRoleVo.convert();
        bossRoleService.save(info);
        // 批量保存角色菜单
        if (bossRoleVo.getMenuList() != null && bossRoleVo.getMenuList().size() > 0) {
            List<BossRoleMenu> list = new ArrayList<>();
            for (String menuId : bossRoleVo.getMenuList()) {
                BossRoleMenu bossRoleMenu = new BossRoleMenu();
                bossRoleMenu.setMenuId(menuId);
                bossRoleMenu.setRoleId(info.getRoleId());
                list.add(bossRoleMenu);
            }
            bossRoleMenuService.batchAdd(list);
        }
        return ResultGenerator.genSuccessResult(info.getRoleId());
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除角色:{}", delete);

        BossRole role = bossRoleService.findById(delete.getId());
        Assert.isNull(role, "角色已被删除！");
        bossRoleService.deleteById(delete.getId());
        // 删除角色菜单和用户角色
        bossRoleMenuService.deleteByRoleId(delete.getId());
        bossUserRoleService.deleteByRoleId(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改角色", notes = "修改角色")
    public Result<String> update(@Validated @RequestBody BossRoleUpdateVo bossRoleVo) {
        logger.info("修改角色:{}", bossRoleVo);
        if(bossRoleVo.getMenuList()==null || bossRoleVo.getMenuList().size()==0) {
            throw BusinessException.msg("角色菜单不能为空");
        }
        BossRole role = bossRoleService.findById(bossRoleVo.getRoleId());
        Assert.isNull(role, "角色已被删除！");
        BossRole info = bossRoleVo.convert();
        bossRoleService.update(info);
        // 批量修改角色菜单（先删除，再新增）
        if (bossRoleVo.getMenuList() != null && bossRoleVo.getMenuList().size() > 0) {
            List<BossRoleMenu> list = new ArrayList<>();
            for (String menuId : bossRoleVo.getMenuList()) {
                BossRoleMenu bossRoleMenu = new BossRoleMenu();
                bossRoleMenu.setMenuId(menuId);
                bossRoleMenu.setRoleId(info.getRoleId());
                list.add(bossRoleMenu);
            }
            bossRoleMenuService.deleteByRoleId(info.getRoleId());
            bossRoleMenuService.batchAdd(list);
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "角色详情", notes = "角色详情")
    public Result<BossRole> detail(@Validated @RequestBody DetailVo detail) {
        logger.info("角色查看详情:{}", detail);

        BossRole sysRole = bossRoleService.findById(detail.getId());
        return ResultGenerator.genSuccessResult(sysRole);
    }

    @PostMapping("/getMenuList")
    @ApiOperation(value = "查询角色菜单", notes = "查询角色菜单")
    public Result<List<BossMenu>> getMenuList(@Validated @RequestBody DetailVo detail) {
        // 查询角色菜单
        List<BossMenu> menus = bossMenuService.getMenuByRoleId(detail.getId());
        return ResultGenerator.genSuccessResult(menus);
    }


    @PostMapping("/list")
    @ApiOperation(value = "查询角色", notes = "查询角色")
    public Result<Page<BossRole>> list(@Validated @RequestBody BossRoleSearchVo search) {
        logger.info("查询角色:{}", search);

        Page<BossRole> list = bossRoleService.getRoleList(search);
        return ResultGenerator.genSuccessResult(list);
    }
}
